#include "eeprom.h"

bool eeprom_write(uint32_t address, const void *data, uint32_t size) {
    
    if (data == NULL || size == 0) {
        return false;
    }

    const uint8_t *byte_data = (const uint8_t *)data;
    
    // 检查地址合法性
    if (address < EEPROM_START_ADDR || address + size > EEPROM_END_ADDR) {
        return false;
    }
    
    // 写入每个字节
    for (uint32_t i = 0; i < size; i++) {
        if (eeprom_write_byte(address + i, byte_data[i]) != EEPROM_OK) {
            return false;
        }
    }
    return true;
}

bool eeprom_read(uint32_t address, void *data, uint32_t size) {

    if (data == NULL || size == 0) {
        return false;
    }
    uint8_t *byte_data = (uint8_t *)data;
    
    // 检查地址合法性
    if (address < EEPROM_START_ADDR || address + size > EEPROM_END_ADDR) {
        return false;
    }
    
    // 读取每个字节
    for (uint32_t i = 0; i < size; i++) {
        byte_data[i] = eeprom_read_byte(address + i);
    }
    return true;
}

uint16_t eeprom_calculate_checksum(const void *data, uint32_t size)
{
    if (data == NULL || size == 0) {
        return 0;
    }
    
    const uint8_t *byte_data = (const uint8_t *)data;
    uint16_t checksum = 0;
    
    for (uint32_t i = 0; i < size; i++) {
        checksum += byte_data[i];
    }
    return checksum;
}